<? 
	include('base.php');
	User::protect();
	$section = 'shows';
	include_class('shows');
	include_class('m2');
	$editors = array('description');

	$sh = Show::get($_GET['id']);
	
	if (!db::isError($sh)) {
		$mi = MediaInstance::get($_GET['media_instance_id']);
		if (!db::isError($mi)) {
			$type = ($mi->getAreaID() == $sh->getAVAreaID()) ? "av" : "photos";
			switch($_GET['task']) {
				case 'update':
					$res = $mi->update($_POST, $sh);
					if (!db::isError($res)) {
						header('Location: show_media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $_GET['media_instance_id']);
					}
					break;
				case 'deactivate':
					$res = $mi->deactivate($sh);
					if (!db::isError($res)) {
						header('Location: show_media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $_GET['media_instance_id']);
					} 
					break;
				case 'activate':
					$res = $mi->activate($sh);
					if (!db::isError($res)) {
						header('Location: show_media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' . $_GET['media_instance_id']);
					}
					break;
				case 'delete':
					$res = $mi->delete($sh);
					if (!db::isError($res)) {
						header('Location: show_media.php?id=' . $_GET['id'] . '&type=' . $type);
					}
					break;
			}
		}
	}
	
	
	$page_title = 'Edit Show Media';
	include('layout/header.php');

if (!db::isError($sh)) {

?>


<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="shows.php">Shows &#62;</a>
	<a href="show_edit.php?id=<?=$_GET['id']?>">View/Edit Show &#62;</a>
	<a href="show_media.php?id=<?=$_GET['id']?>&amp;type=<?=$type?>">Media&nbsp;&#62;</a>
	Edit Media&nbsp;&#62;
</div>

<? 
	$canEdit = User::isAdmin();
	if ($res) { // it's an error
		if (db::isError($res)) {
			$res->outputList();
		}
	}
	if (!db::isError($mi)) {
		if ($_GET['task'] == 'edit' && $canEdit) { 
		
			$exitURL = 'show_media_edit.php?id=' . $_GET['id'] . '&media_instance_id=' .  $_GET['media_instance_id'];
			include('layout/media_edit.php');				
			
			
		} else {
		
			$exitURL = 'show_media.php?id=' . $_GET['id'] . '&type=' . $type;
			include('layout/media_view.php'); 	
		
		}
	
	} else {
		
		$mi->outputDialog('Return to Shows','show.php?id=' . $_GET['id']);
		
	}
	
			
} else { ?>

<div id="breadcrumb">
	<a href="index.php">Audition &#62;</a>
	<a href="live.php">Live &#62;</a>
	<a href="shows.php">Shows &#62;</a>
	Invalid Entry&nbsp;&#62;
</div>

<?
	$sh->outputDialog('Return to Shows','shows.php','Please specify a valid show ID.');

} ?>

<? include('layout/footer.php'); ?>